package com.letv.android.client.album.controller;

import com.letv.android.young.client.R;
import com.letv.adlib.managers.status.video.IVideoStatusInformer;
import com.letv.adlib.sdk.types.AdElementMime;
import com.letv.ads.ADPlayFragment;
import com.letv.ads.ADPlayFragment.PauseAdsListener;
import com.letv.ads.ADPlayFragment.PlayAdListener;
import com.letv.ads.ADPlayFragment.VipViewCallBack;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.android.client.fragment.AlbumAdFragment;
import com.letv.android.client.webview.LetvWebViewActivity;
import com.letv.business.flow.album.AlbumPlayFlow;
import com.letv.business.flow.album.listener.PlayAdFragmentListener;
import com.letv.core.constant.LetvConstant;
import com.letv.core.constant.PlayConstant;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.util.PageIdConstant;

import java.util.ArrayList;

/**
 * 播放广告控制器
 *
 * @author zhuqiao
 */
public class AlbumRoomPlayAdController extends VipViewCallBack implements PlayAdFragmentListener, PauseAdsListener,
    PlayAdListener {
    private AlbumPlayRoomActivity mActivity;

    /**
     * 广告fragment
     **/
    private AlbumAdFragment mPlayAdFragment = new AlbumAdFragment();

    public AlbumRoomPlayAdController(AlbumPlayRoomActivity activity) {
        mActivity = activity;

        mPlayAdFragment.setViewCallBack(this);
        mPlayAdFragment.setPauseAdsListener(this);
        mPlayAdFragment.setAdListener(this);
    }

    public void setLaunchMode(int launchMode, boolean is4d, boolean isDobly) {
        if (launchMode == PlayConstant.LAUNCH_MODE_VIDEO || is4d || isDobly) {
            mPlayAdFragment.setAdsViewHalfFullBtnVisible(false);
        } else {
            mPlayAdFragment.setAdsViewHalfFullBtnVisible(true);
        }
    }

    public ADPlayFragment getAdFragment() {
        return mPlayAdFragment;
    }

    // ------------------- 广告VIP回调 -------------------
    @Override
    public void onClick() {
        //TODO 广告点击跳过广告
        LetvWebViewActivity.launch(mActivity,
                LetvConstant.APP_DOWNLOAD_URL,
                mActivity.getResources().getString(R.string.moreaboutusactivity_webtitle));
//        if (mActivity.getFlow() == null)
//            return;
//
//        if (PreferencesManager.getInstance(mActivity).isLogin() && PreferencesManager.getInstance(mActivity).isVip()) {
//            mActivity.getFlow().onClickShipAd();
//            mPlayAdFragment.pause();
//            mPlayAdFragment.setMobileNetBg(false);
//            mActivity.getFlow().getFrontAd();
//        } else {
//            VipProductsActivity.launchFromPlay(mActivity, mActivity.getString(R.string.pim_vip_good_title), mActivity
//                    .getFlow().getAid(), mActivity.getFlow().getVid());
//        }

        // 统计
        LogInfo.LogStatistics("广告VIP回调");
        StatisticsUtils.staticticsInfoPost(mActivity, "0", "c61", null, 1, null,
            UIsUtils.isLandscape(mActivity) ? PageIdConstant.fullPlayPage : PageIdConstant.halpPlayPage,
            String.valueOf(mActivity.getFlow().getCid()), null, String.valueOf(mActivity.getFlow().getVid()), null,
            null);
    }

    @Override
    public void ads3G2GClick() {
        mActivity.getController().start();
    }

    @Override
    public int getVideoCurrentTime() {
        if (mActivity.getAlbumPlayFragment() != null && mActivity.getAlbumPlayFragment().getVideoView() != null) {
            return mActivity.getAlbumPlayFragment().getVideoView().getCurrentPosition();
        }

        return 0;
    }

    @Override
    public void hasAdsVideos(int count) {
        if (mActivity.getFlow() != null) {
            mActivity.getFlow().setHasAd(count > 0);
            mActivity.getFlow().setAdCount(count);
            LogInfo.log("zhuqiao", "hasAdsVideos:" + count);
        }
    }

    /**
     * 广告缓冲完之后，提示客户端可以开始缓冲正式视频了
     */
    @Override
    public void getMainClientBackGroundStart() {
        if (mActivity.getFlow() != null) {
            mActivity.getFlow().onBackgroundStart();
        }
    }

    @Override
    public void setHalfOrFullScreen(boolean isHalf, boolean isBtnHalf) {
        //TODO 广告按下back按钮
        // 广告按下back按钮
        AlbumController controllerUtils = mActivity.getController();

//        if (!isBtnHalf && LetvApplication.getInstance().getLiveLunboBundle() != null) {
//            controllerUtils.back();
//            return;
//        }
//        if (mActivity.getFlow() != null) {
//            if (mActivity.getFlow() instanceof AlbumPlayTopicFlow) {
//                // 专题
//                if (isHalf) {
//                    controllerUtils.full();
//                } else {
//                    controllerUtils.half();
//                }
//                return;
//            }
//            if (mActivity.getFlow().getLaunchMode() == PlayConstant.PLAY_ALBUM
//                    || mActivity.getFlow().getLaunchMode() == PlayConstant.PLAY_DOWNLOAD) {
//                if (isHalf) {
//                    controllerUtils.full();
//                } else {
//                    if (!controllerUtils.fullBack()) {
//                        controllerUtils.half();
//                    }
//                    controllerUtils.half();
//                }
//                return;
//            }
//        }
        controllerUtils.full();

        controllerUtils.back();
    }

    @Override
    public void onHalfBackDown() {
        mActivity.getController().back();
        StatisticsUtils.staticticsInfoPost(mActivity, "0", "h22", "0005", 1, null, PageIdConstant.halpPlayPage, null,
            null, null, null, null);
    }

    // ------------------- 暂停广告回调 -------------------

    @Override
    public void pauseAdsIsShow() {
        LogInfo.log("zhuqiao", "pauseAdsIsShow");
        if (mActivity.getFlow() == null)
            return;

        AlbumPlayFlow flow = mActivity.getFlow();

        if (!AlbumPlayRoomActivity.sIsShowingLongwatch) {
            flow.setIsPauseAdIsShow(true);
            if (UIsUtils.isLandscape(mActivity)) {
                if (mActivity.getRecommendController() != null) {
                    mActivity.getRecommendController().hideRecommendTipView();
                }
                if (mActivity.getFullControllerFragment() != null) {
                    mActivity.getFullControllerFragment().setInterceptClick();
                }
            }
        }
    }

    @Override
    public void closePauseAdCallBack() {
        LogInfo.log("zhuqiao", "closePauseAdCallBack");
        if (mActivity.getFlow() != null) {
            mActivity.getFlow().setIsPauseAdIsShow(false);
        }
        if (mActivity.getController() != null) {
            mActivity.getController().start();
        }
    }

    // ------------------- 广告监听回调 -------------------

    @Override
    public void onFinish(boolean isFinishByHand) {
        if (mActivity.getFlow() != null) {
            mActivity.getFlow().onAdsFinish(isFinishByHand);
        }
    }

    @Override
    public void onM3U8(ArrayList<AdElementMime> ads) {
        // do nothing
    }


    @Override
    public void onStartPlay() {
        if (mActivity.getFlow() != null) {
            mActivity.getFlow().startPlayAd(mPlayAdFragment.getAdsPlayFirstFrameTime());
        }
    }

    // ------------------- 广告fragment对外提供的接口回调 -------------------
    @Override
    public void pause() {
        mPlayAdFragment.pause();
    }

    @Override
    public void setPauseAd(boolean pause) {
        mPlayAdFragment.setPauseAd(pause);
    }

    @Override
    public void setADPause(boolean pause) {
        mPlayAdFragment.setADPause(pause);
    }

    @Override
    public void stopPlayback(boolean notifyOnFinish) {
        mPlayAdFragment.stopPlayback(notifyOnFinish);
    }

    @Override
    public void onResume() {
        mPlayAdFragment.onResume();
    }

    @Override
    public void cancelRequestFrontAdTask() {
        mPlayAdFragment.cancelRequestFrontAdTask();
    }

    @Override
    public void getOfflineFrontAd(int cid, long aid, long vid, String mmsid, String uuid, String uid, String vlen,
                                  String py, String ty, boolean isVipVideo, boolean disableAvd, boolean toShowLoading, boolean isOfflineAds) {
        mPlayAdFragment.getOfflineFrontAd(cid, aid, vid, mmsid, uuid, uid, vlen, py, ty, isVipVideo, disableAvd,
            toShowLoading, isOfflineAds);
    }

    @Override
    public void getDemandFrontAd(int cid, long aid, long vid, String mmsid, String uuid, String uid, String vlen,
                                 String py, String ty, boolean isSupportM3U8, boolean isVipVideo, boolean disableAvd, boolean toShowLoading,
                                 boolean isWoOrderUser, boolean isOpenCde) {
        mPlayAdFragment.getDemandFrontAd(cid, aid, vid, mmsid, uuid, uid, vlen, py, ty, isSupportM3U8, isVipVideo,
            disableAvd, toShowLoading, isWoOrderUser, isOpenCde);
    }

    @Override
    public void getDemandPauseAd(int cid, long aid, long vid, String mmsid, String uuid, String uid, String vlen,
                                 String py, String ty) {
        mPlayAdFragment.getDemandPauseAd(cid, aid, vid, mmsid, uuid, uid, vlen, py, ty);
    }

    @Override
    public void closePauseAd() {
        mPlayAdFragment.closePauseAd();
    }

    @Override
    public boolean isPlaying() {
        return mPlayAdFragment.isPlaying();
    }

    @Override
    public IVideoStatusInformer getIVideoStatusInformer() {
        return mPlayAdFragment.getIVideoStatusInformer();
    }

    @Override
    public void setIVideoStatusInformer(IVideoStatusInformer format) {
        mPlayAdFragment.setIVideoStatusInformer(format);
    }
}
